home *** CD-ROM | disk | FTP | other *** search
- Option Strict Off
- Option Explicit On
- Module NNTrainAndGetResponse
-
- Public resetNetwork As Boolean
-
- Function TrainNetwork() As Object
-
- 'DESCRIPTION: This subroutine illustrates how the DLL generated by the Custom
- 'Solution Wizard can be used for training in a Visual Basic application. The
- 'subroutine trains the network DLL saving the best weights to a specified file for
- 'later use. Step by step instructions are given on how to use the generated DLL
- 'for this purpose. Read through comments carefully considering all of the IMPLEMENTATION
- 'NOTES as you get to them.
-
- 'NOTE: This subroutine only applies to users of the Developers level of the Custom
- 'Solution Wizard. The Developers level allows the user to generate learning DLLs,
- 'whereas all other levels can only generate recall DLLs.
-
- 'Step 1: Create a new neural network object of the NSLearningNetwork type.
- '-------------------------------------------------------------------------
- Dim nn As New NeuroSolutions.NSLearningNetwork()
-
- 'Step 2: Set the pathName of the generated network DLL.
- '------------------------------------------------------
- 'IMPLEMENTATION NOTES: The DLL_PATH_NAME below is defined in the Globals module as the
- 'path to the newly generated DLL.
- 'MORE INFO: The call to dllPathName will fail if you do not have the Developers
- 'level of the Custom Solution Wizard or if you generated the DLL using a recall
- 'NeuroSolutions breadboard. If either of these cases apply, you will only be able
- 'to create NSRecallNetwork objects (which are not trainable).
- nn.dllPathName = DLL_PATH_NAME
-
- 'Step 3: Load the initial network weights. (Optional)
- '----------------------------------------------------
- 'IMPLEMENTATION NOTES: The WEIGHTS_PATH_NAME below is defined in the Globals module as
- 'the path to the weights file which was saved when the DLL was generated.
- 'MORE INFO: This step is not necessary. If this step is not performed, the
- 'network will simply start with random initial weights.
- nn.loadWeights(WEIGHTS_PATH_NAME)
-
- 'Resets the network (randomizes the network weights, etc.) if the user has so indicated
- 'via the "Reset Network before Training" check box on the VBShellDlg form
- If resetNetwork = True Then
- nn.resetNetwork()
- End If
-
- 'Step 4: Define the input data.
- '------------------------------
- 'IMPLEMENTATION NOTES: The following loads the training input data from your initial
- 'network configuration into the array inputData. The training input data was saved
- 'to a file during DLL generation. The location of this file is specified in
- 'the Globals module. There are a number of ways to load your inputData, including
- 'hard-coding, user input, and file access. This implementation reads the data from
- 'a text file using DAO (other file types supported by DAO can also be used - see your
- 'Visual Basic documentation or consult the Microsoft Developer Network for more
- 'information on DAO). This approach maximizes flexibility for both data access and
- 'manipulation.
- 'MORE INFO: Note that the number of inputs in the input data must match the number of inputs
- 'expected by the generated DLL. Also, there can be any number of exemplars in the
- 'input data as long as the number of exemplars in the input data matches the number
- 'of exemplars in the desired data.
- Dim inputData As Object
- inputData = createDataArrayFromFile(DATA_PATH, INPUT_FILE_NAME)
-
- 'Step 5: Send the input data to the network DLL.
- '-----------------------------------------------
- nn.inputData = inputData
-
- 'Step 6: Define the desired data.
- '--------------------------------
- 'IMPLEMENTATION NOTES: The following loads the training desired data from your initial
- 'network configuration into the array desiredData. The training desired data was saved
- 'to a file during DLL generation. The location of this file is specified in
- 'the Globals module. There are a number of ways to load your desiredData, including
- 'hard-coding, user input, and file access. This implementation reads the data from
- 'a text file using DAO (other file types supported by DAO can also be used - see your
- 'Visual Basic documentation or consult the Microsoft Developer Network for more
- 'information on DAO). This approach maximizes flexibility for both data access and
- 'manipulation.
- 'MORE INFO: Note that the number of outputs in the desired data must match the number of outputs
- 'expected by the generated DLL. Also, there can be any number of exemplars in the
- 'desired data as long as the number of exemplars in the desired data matches the number
- 'of exemplars in the input data.
- Dim desiredData As Object
- desiredData = createDataArrayFromFile(DATA_PATH, DESIRED_FILE_NAME)
-
- 'Step 7: Send the desired data to the network DLL.
- '-------------------------------------------------
- nn.desiredData = desiredData
-
- 'Step 8: Enable the automatic saving of the best weights. (Optional)
- '-------------------------------------------------------------------
- 'MORE INFO: This step is not necessary. The default value of this property is
- 'always False. Setting it to True will cause the best network weights to be saved
- 'during training. If this property is set to True, be sure to set the
- 'bestWeightsPathName to a valid pathName (See Step 9).
- nn.saveBestWeightsEnabled = True
-
- 'Step 9: Set the pathName for saving the best weights. (Optional)
- '----------------------------------------------------------------
- 'IMPLEMENTATION NOTES: The BEST_WEIGHTS_PATH_NAME below is defined in the
- 'Globals module.
- 'MORE INFO: The specified pathName will be used for saving the best weights during
- 'training. This bestWeightsPathName property must be set to a valid pathName
- 'if saveBestWeightsEnabled has been set to True. Otherwise, setting this property
- 'is optional.
- nn.bestWeightsPathName = BEST_WEIGHTS_PATH_NAME
-
- 'Step 10: Train the network.
- '---------------------------
- 'IMPLEMENTATION NOTES: The NUMBER_OF_EPOCHS is determined from the initial network configuration
- 'and is specified in the Globals module.
- nn.train(NUMBER_OF_EPOCHS)
-
- 'Step 11: Get the best cost achieved during training.
- '----------------------------------------------------
- Dim bestCost As Single
- bestCost = nn.bestCost
- TrainNetwork = bestCost
-
- 'Step 12: Release the neural network object.
- '-------------------------------------------
- nn = Nothing
-
- End Function
-
- Function GetNetworkResponse() As Object
-
- 'DESCRIPTION: This function illustrates how the DLL generated by the Custom
- 'Solution Wizard can be used for getting the network response (output) in a Visual
- 'Basic application. Step by step instructions are given on how to use the
- 'generated DLL for this purpose. Read through comments carefully considering all
- 'of the IMPLEMENTATION NOTES as you get to them. This function returns the network
- 'response.
-
- 'NOTE: This subroutine applies to users of any level of the Custom Solution Wizard.
-
- 'Step 1: Create a new neural network object of the NSRecallNetwork type.
- '-----------------------------------------------------------------------
- 'MORE INFO: You could create an NSLearningNetwork object network instead, if you
- 'have the Developers level of the Custom Solution Wizard and used it along with a
- 'NeuroSolutions breadboard capable of learning to generate the DLL.
- Dim nn As New NeuroSolutions.NSRecallNetwork()
-
- 'Step 2: Set the pathName of the generated network DLL.
- '------------------------------------------------------
- 'IMPLEMENTATION NOTES: The DLL_PATH_NAME below is defined in the Globals module as the
- 'path to the newly generated DLL.
- nn.dllPathName = DLL_PATH_NAME
-
- 'Step 3: Load the saved network weights.
- '---------------------------------------
- 'IMPLEMENTATION NOTES: The BEST_WEIGHTS_PATH_NAME below is defined in the Globals module.
- 'The initial best weights file is an exact copy of the weights file that was saved
- 'when the DLL was generated. However, the best weights file will change with each
- 'run of the TrainNetwork function if the network is reset before training.
- nn.loadWeights(BEST_WEIGHTS_PATH_NAME)
-
- 'Step 4: Define the input data.
- '------------------------------
- 'IMPLEMENTATION NOTES: The following loads the training input data from your initial
- 'network configuration into the array inputData. The training input data was saved
- 'to a file during DLL generation. The location of this file is specified in
- 'the Globals module. There are a number of ways to load your inputData, including
- 'hard-coding, user input, and file access. This implementation reads the data from
- 'a text file using DAO (other file types supported by DAO can also be used - see your
- 'Visual Basic documentation or consult the Microsoft Developer Network for more
- 'information on DAO). This approach maximizes flexibility for both data access and
- 'manipulation.
- 'MORE INFO: Note that the number of inputs in the input data must match the number of inputs
- 'expected by the generated DLL.
- Dim inputData As Object
- inputData = createDataArrayFromFile(DATA_PATH, INPUT_FILE_NAME)
-
- 'Step 5: Send the input data to the network DLL.
- '-----------------------------------------------
- nn.inputData = inputData
-
- 'Step 6: Get the network response (output).
- '------------------------------------------
- 'MORE INFO: The network response is assigned to the return value of GetNetworkResponse
- 'function.
- GetNetworkResponse = nn.getResponse
-
- 'Step 7: Release the neural network object.
- '------------------------------------------
- nn = Nothing
-
- End Function
-
- Function createDataArrayFromFile(ByRef filePath As String, ByRef fileName As String) As Object
-
- 'The following describes general information about the use of this function.
- 'Usually, no further action is required. However, this information is
- 'provided to explain how use this function on a different computer or with
- 'a different dataset.
-
- 'In order to use this function, you must have Microsoft DAO 3.6 / Jet 4.0 installed
- 'on your machine (installed during the Custom Solution Wizard installation if
- 'not already present).
-
- 'You must also have a reference to the Microsoft DAO 3.6 Object Library.
- 'To add this reference, within Visual Basic, goto to Project menu, click
- 'References, then place a check next to the Microsoft DAO 3.6 Object Library
- 'item in the resulting list box. Note: This reference has already been added for
- 'this project.
-
- 'Furthermore, use of this function requires that you have created a schema.ini
- 'file defining the format of your data file and placed this file in the same
- 'directory as your data file (already done for the training data taken from
- 'the breadboard used to create the DLL).
-
- 'Consider for example the following tab delimited XOr input data:
- 'x y
- '0 0
- '0 1
- '1 0
- '1 1
-
- 'If this XOr input data were placed within a text file named "XOr.txt", a "schema.ini"
- 'file (placed in the same directory as the "XOr.txt" file) with the following
- 'information could be used to correctly read this file:
-
- '[XOr.txt]
- 'ColNameHeader = True
- 'Format = TabDelimited
- 'MaxScanRows = 0
- 'CharacterSet = ANSI
-
- 'For more information on using schema.ini files see the Reading Data from Text
- 'Files topic in the Custom Solution Wizard help.
- Dim DAODBEngine As New DAO.DBEngine()
- Dim dbs As DAO.Database
- dbs = DAODBEngine.OpenDatabase(filePath, False, True, "Text;")
-
- Dim rst As DAO.Recordset
- rst = dbs.OpenRecordset(fileName, DAO.RecordsetTypeEnum.dbOpenSnapshot)
- rst.MoveLast()
- Dim numberOfRecords As Integer
- numberOfRecords = rst.RecordCount
- rst.MoveFirst()
- createDataArrayFromFile = rst.GetRows(numberOfRecords)
-
- rst.Close()
- dbs.Close()
-
- End Function
- End Module